﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfAGVSystem.Common;
using static WpfAGVSystem.Common.EnumDefinitions;

namespace WpfAGVSystem.ViewModels.SamllViewModel
{
   public class SamllVMBase: BindableBase
    {
        private SettingPara Setting;
        public static List<SamllVMBase> listSamllVMBase = new List<SamllVMBase>();
        public SamllVMBase(SettingPara settingPara) 
        {
            Setting = settingPara;
            Height = smallSize.Height;
            Width = smallSize.Width;
            listSamllVMBase.Add(this);
        }
        /// <summary>
        /// 用来划分上下 也可以划分左右
        /// </summary>
        public int InterfaceMapType { get; set; } = 0;
        private Point location;
        public Point Location
        {
            get { return location; }
            set
            {
                location = value;
                Top = (location.Y - Height / 2) + InterfaceMapType * Setting.MapActualHeight;
                Left = location.X - Width / 2;
            }
        }
        public static bool DragMode { get; set; }
        public string DeviceID { get; set; }

        private bool connected = false;
        //报警状态  
        public bool Connected
        {
            get { return connected; }
            set
            {
                connected = value;
                Disconnected = !value;
            }
        }


        public bool Disconnected { get; set; } = true;
        public bool LowBattery { get; set; } = false;
        public bool AGVAlarm { get; set; } = false;
        public bool DispatchAlarm { get; set; } = false;


        /// <summary>
        /// 物料状态
        /// </summary>
        //public string MaterialStatus { get; set; }
        /// <summary>
        /// 置信度
        /// </summary>
        public string Confidence { get; set; }
        public string Delayed { get; set; }
        public double AGVSpeed { get; set; }

        public string DrivingStatus { get; set; } = StringProcess.STOPSTR;
        Point prePoint;
        public void Click(object sender, MouseButtonEventArgs e)
        {
            PineSize = (PineSizeType)(((int)PineSize + 1) % 2);
            if (PineSize == PineSizeType.Big)
            {
                ShrinkOthersOfSameType();
            }
            if (DragMode)
            {
                var uc = (UserControl)sender;
                var canvas = ((UserControl)sender).Parent as Canvas;
              //  prePoint = e.GetPosition(canvas).ExConvert();
            }
        }
        private void ShrinkOthersOfSameType()
        {
            //var type = DeviceID.ExGetDeviceTypeByDeviceID();
            //if (type == null) { return; }
            //foreach (var item in listBaseVM_Dev)
            //{
            //    if (item.PineSize == PineSizeType.Big && item.DeviceID != DeviceID)
            //    {
            //        var typeItem = item.DeviceID.ExGetDeviceTypeByDeviceID();
            //        if (typeItem != null && type.Intersect(typeItem) != null)
            //        {
            //            item.PineSize = PineSizeType.Small;
            //        }
            //    }
            //}
        }
        public void OnUserControl_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && DragMode)
            {
                var uc = (UserControl)sender;
                var canvas = ((UserControl)sender).Parent as Canvas;
                var currentPoint = e.GetPosition(canvas);
                //Location = Add(Location, DeltaPt(currentPoint.ExConvert(), prePoint));
                //prePoint = currentPoint.ExConvert();
                //DevPosMgr.SetDevicePostion(DeviceID, Location);
            }
        }
        private Point Add(Point currentPt, Point deltaPt)
        {
            return new Point(Location.X + deltaPt.X, Location.Y + deltaPt.Y);
        }
        private Point DeltaPt(Point currentPt, Point anotherPt)
        {
            return new Point(currentPt.X - anotherPt.X, currentPt.Y - anotherPt.Y);
        }
        public void LoadProductTypeInfo()
        {
            string proTypeSetting = "";
            //Database.GetProductType(DeviceID, ref proTypeSetting, ref productType_Current);
            ProductType_Setting = proTypeSetting;
        }



        public string ProductType_Setting { get; set; }
        private string productType_Current;
        public string ProductType_Current
        {
            get => productType_Current;
            set
            {
                productType_Current = value;
                //Database.SetProductType(DeviceID, null, ProductType_Current);
            }
        }
        public string DeviceName { get; set; } = "Unknow";
        private bool bAlive = false;
        public bool BAlive
        {
            get { return bAlive; }
            set
            {
                if (value == bAlive) { return; }
                bAlive = value;
                BDead = !bAlive;
            }
        }
        public bool BDead { get; set; } = true;
        public bool BAlarm { get; set; } = false;

        public bool BVisual { get; set; } = true;
        public bool BHide { get; set; } = false;
        public double Left { get; private set; }
        public double Top { get; private set; }
        public int ZIndex { get; set; }
        public static int MaxZIndex
        {
            get
            {
                int idx = 0;
                foreach (var item in listSamllVMBase)
                {
                    if (item.ZIndex > idx) { idx = item.ZIndex; }
                }
                return idx + 1;
            }
        }
        public double Height { get; set; }
        public double Width { get; set; }
        public Size bigSize = new Size(200, 150);
        public Size smallSize = new Size(30, 25);


        private PineSizeType pineSize = PineSizeType.Small;

        public PineSizeType PineSize
        {
            set
            {
                if (pineSize == value) return;
                pineSize = value;
                if (pineSize == PineSizeType.Small)
                {
                    Height = smallSize.Height;
                    Width = smallSize.Width;
                    Top += (bigSize.Height - smallSize.Height) / 2;
                    Left += (bigSize.Width - smallSize.Width) / 2;
                }
                else if (pineSize == PineSizeType.Big)
                {
                    Height = bigSize.Height;
                    Width = bigSize.Width;
                    Top -= (bigSize.Height - smallSize.Height) / 2;
                    Left -= (bigSize.Width - smallSize.Width) / 2;

                }
            }
            get { return pineSize; }
        }
    }
}
